package com.fxu.gen;

import cn.hutool.core.util.StrUtil;
import com.fxu.framework.biz.base.Limits;
import com.fxu.framework.core.base.Constant;
import com.fxu.framework.core.enums.*;
import com.fxu.gen.enums.ClnType;
import com.fxu.gen.enums.FormType;
import com.fxu.gen.enums.QueryType;
import com.fxu.gen.enums.TplEnum;
import com.fxu.tpl.entity.Column;
import com.fxu.tpl.entity.Link;
import com.fxu.tpl.entity.Table;
import com.fxu.tpl.enums.ColumnTypeEnum;

import java.io.File;

/**
 * 基于vben-admin
 * @author fangxu
 * @version 2023-02-26
 */
public final class VbenVue3 extends Base {
	public VbenVue3(Table table, File destFile) {
		super(table, TplEnum.VbenVue3, destFile);
	}

	@Override
	public String doReplace() {
		String priKey = getPriKeyClnName(columns);
		String curdSchemas = getCrudSchemas(priKey, 1);

		fileTxt = fileTxt.replace("name: 'HdbUser',", "name: '"+tableBean+"',");
		fileTxt = fileTxt.replace("const name: string = '用户'", "const name: string = '"+table.getCmmt()+"'");
		fileTxt = fileTxt.replace("const bean: string = 'hdb.hdbUser'",
				"const bean: string = '"+table.getModule().getName()+"."+AutoUtil.firstLowerBean(tableBean)+"'");
		fileTxt = fileTxt.replace("const priKey: string = 'id'", "const priKey: string = '"+ priKey +"'");
		fileTxt = fileTxt.replace("const apiParams: ApiParams = { showFlags: 'roleList[name]', params: {}}",
				"const apiParams: ApiParams = { " + getShowFlags() + "params: {}}");
		fileTxt = fillByKey(fileTxt, "reactive<CrudSchema[]>([", "  ]) // crudSchemas", curdSchemas);

		// 导入导出
		if (!isTrue(table.getIsExport())) {
			fileTxt = removeLine(fileTxt, 1, "import ExcelExport from '/@/views/comp/ExcelExport.vue'");
			fileTxt = removeLine(fileTxt, 4, "<excel-export :bean=\"bean\" :pri-key=\"priKey\"");
			fileTxt = removeLine(fileTxt, 3, "const resTotal = ref<number>(0)");
			fileTxt = removeLine(fileTxt, 4, "resTotal.value = total");
			fileTxt = removeLine(fileTxt, 4, "resTotal,");
			fileTxt = removeLine(fileTxt, 4, "reqParams,");
			fileTxt = fileTxt.replace(", ExcelExport", "");
		}
		if (!isTrue(table.getIsImport())) {
			fileTxt = removeLine(fileTxt, 4, "<excel-import :bean=\"bean\" :title=\"name\" :columns=\"tableColumns\" />");
			fileTxt = removeLine(fileTxt, 1, "import ExcelImport from '/@/views/comp/ExcelImport.vue'");
			fileTxt = fileTxt.replace(", ExcelImport", "");
		}
		if (!isTrue(table.getIsExport()) && !isTrue(table.getIsImport())) {
			fileTxt = removeLine(fileTxt, 4, "tableColumns,");
		}

		// 隐藏、显示处理
		if (BaseEnum.hasAny(columns, cln -> cln.getLimits() != null && ShowStatus.is(cln.getLimits()))) {
			// 原封不动
		} else if (BaseEnum.hasAny(columns, cln -> cln.getLimits() != null && ShowStrStatus.is(cln.getLimits()))) {
			fileTxt = fileTxt.replace("v-if=\"record.status === 2\"", "v-if=\"record.status === 'SHOW'\"")
					.replace("handleStatus(record, 1, '隐藏')", "handleStatus(record, 'HIDE', '隐藏')");
			fileTxt = fileTxt.replace("v-if=\"record.status === 1\"", "v-if=\"record.status === 'HIDE'\"")
					.replace("handleStatus(record, 2, '显示')", "handleStatus(record, 'SHOW', '显示')");
			fileTxt = fileTxt.replace("values['status'] = 2", "values['status'] = 'SHOW'");
		} else {
			//import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons-vue';
			fileTxt = removeLine(fileTxt, 5, "<a-button v-if=\"record.status === 2\"");
			fileTxt = removeLine(fileTxt, 5, "<a-button v-if=\"record.status === 1\"");
			fileTxt = removeLine(fileTxt, 5, "values['status'] = 2");
			fileTxt = fileTxt.replace("width: 225,","width: 138,");
			fileTxt = fileTxt.replace(", EyeOutlined, EyeInvisibleOutlined","");
			fileTxt = removeLine(fileTxt, 1, "import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons-vue';");
		}
		if (BaseEnum.hasAny(columns, cln -> cln.getLimits() != null && cln.getLimits().contains("DELETE"))) {
			fileTxt = fileTxt.replace("handleStatus(record, 0, '删除')","handleStatus(record, 'DELETE', '删除')");
		}

		// 特殊字段处理
		String specialColumn = getSpecialColumn(4);
		if (StrUtil.isNotEmpty(specialColumn)) {
			fileTxt = fileTxt.replace("        <template v-if=\"column.key === 'action'\">",
					specialColumn + "\n        <template v-if=\"column.key === 'action'\">");
		}

		// 树形表格
		if (AutoUtil.hasColumn(columns, Constant.NEW_PARENT_ID)) {
			fileTxt = fileTxt.replace("// isTreeTable: true,", "isTreeTable: true,");
			fileTxt = fileTxt.replace("// pagination: false,", "pagination: false,");
			fileTxt = fileTxt.replace("// striped: false,", "striped: false,");
		}

		// 级联列表 一对多链接对象
		String innerSchemas = getCascadeSchemas(1);
		if (StrUtil.isNotEmpty(innerSchemas)) {
			fileTxt = fillByKey(fileTxt, "<CascadeSchema[]>([", "  ]) // innerSchemas", innerSchemas);
		} else {
			fileTxt = fileTxt.replace(", CascadeSchema", "");
			fileTxt = fileTxt.replace(", InfoCircleOutlined", "");
			fileTxt = removeLine(fileTxt, 1, "const innerSchemas = reactive<CascadeSchema[]>([");
			fileTxt = removeLine(fileTxt, 1, "]) // innerSchemas");
			fileTxt = removeLine(fileTxt, 4, "innerSchemas,");
			fileTxt = removeLine(fileTxt, 5, "<a-button @click=\"handleOpen(record, true)\"><InfoCircleOutlined /></a-button>");
			fileTxt = removeLine(fileTxt, 4, "if (detail) { openDetail(true, { record }); return }");
			fileTxt = fileTxt.replace(", detail: boolean = false", "");
			fileTxt = fileTxt.replace(", DetailModal", "");
			fileTxt = removeLine(fileTxt, 4, "registerDetail,");
			fileTxt = removeLine(fileTxt, 2, "<DetailModal :title=\"name\" :cascades=\"innerSchemas\" @register=\"registerDetail\" />");
			fileTxt = removeLine(fileTxt, 1, "import DetailModal from '/@/views/comp/DetailModal.vue'");
			fileTxt = removeLine(fileTxt, 3, "const [registerDetail, { openModal: openDetail }] = useModal();");
			fileTxt = fileTxt.replace(" :cascades=\"innerSchemas\"", "");
		}

		// text字段展示
		String bigColumn = getBigColumn(2);
		if (StrUtil.isNotEmpty(bigColumn)) {
			fileTxt = fillByKey(fileTxt, "<template #expandedRowRender=\"{ record }\">",
					"      </template><!-- expandedRowRender -->", bigColumn);
		} else {
			int start = fileTxt.indexOf("      <template #expandedRowRender=\"{ record }\">");
			int end = fileTxt.indexOf("</template><!-- expandedRowRender -->\n") + "</template><!-- expandedRowRender -->\n".length();
			if (start > 0 && end > start) {
				fileTxt = fileTxt.substring(0, start) + fileTxt.substring(end);
			}
			fileTxt = fileTxt.replace("width: 225,","width: 156,");
			fileTxt = fileTxt.replace("width: 138,","width: 156,");
		}
		return fileTxt;
	}

	// 级联对象
	private String getBigColumn(int tab) {
		StrBuf innerSchemas = new StrBuf(true);
		columns.stream().filter(cln -> ColumnTypeEnum.find(cln.getType()) == ColumnTypeEnum.Text
				|| ColumnTypeEnum.find(cln.getType()) == ColumnTypeEnum.Blob).forEach(cln -> {
			innerSchemas.append(tab + 2, "<div style=\"padding: 10px 15px; background: #fff; margin-bottom: 10px;\">{1}:", cln.getCmmt());
			innerSchemas.append(tab + 3, "<div style=\"border-top: solid #eee; padding-top: 6px; margin-top: 6px;\">{{ record.{1} }}</div>", AutoUtil.firstLowerBean(cln.getName()));
			innerSchemas.append(tab + 2, "</div>");
		});
		return innerSchemas.moveLast().toString();
	}

	// 级联对象
	private String getCascadeSchemas(int tab) {
		StrBuf innerSchemas = new StrBuf(true);
		for (Link link : links) {
			if (!(link.getLinkTable() != null && link.getLinkColumn() != null && isTrue(link.getLinkSave())
					&& isTrue(link.getIsList()) && link.getLinkTable().getColumnList() != null)) {
				continue;
			}
			String linkTableName = getLinkPropName(link);
			String priKey = getPriKeyClnName(link.getLinkTable().getColumnList());
			String linkColumnName = AutoUtil.firstLowerBean(link.getLinkColumn().getName());

			innerSchemas.append(tab + 1, "{");
			innerSchemas.append(tab + 2, "label: '{1}列表',", link.getLinkTable().getCmmt());
			innerSchemas.append(tab + 2, "field: '{1}List',", linkTableName);
			innerSchemas.append(tab + 2, "priKey: '{1}',", priKey);
			innerSchemas.append(tab + 2, "datas: [],");
			innerSchemas.append(tab + 2, "columns: [");
			for (Column cln : link.getLinkTable().getColumnList()) {
				// 表格和输入都不显示不输出
				if ((!isTrue(cln.getFormShow()) && !isTrue(cln.getListShow()))
						|| priKey.equals(cln.getName()) || linkColumnName.equals(cln.getName())) {
					continue;
				}
				innerSchemas.append(tab + 3, "{");

				String form = getFormItem(cln, priKey, tab + 2, true);
				form = form.replace("component:", "editComponent:");
				form = form.replace("componentProps:", "editComponentProps:");
				innerSchemas.append(form + "\n");
				innerSchemas.append(tab + 3, "},");
			}
			innerSchemas.append(tab + 2, "],");
			innerSchemas.append(tab + 1, "},");
		}
		return innerSchemas.moveLast().toString();
	}

	// 获取查询表单
	private String getShowFlags() {
		StringBuilder builder = new StringBuilder();
		columns.stream().filter(cln -> cln.getLinkTable() != null && !Constant.NEW_TENANT_ID.equals(cln.getName())).forEach(cln -> {
			String name = cln.getName().replace("_ids", "_list").replace("_id", "");
			builder.append(",").append(AutoUtil.firstLowerBean(name)).append("[name]");
		});
		for (Link link : links) {
			if (!(link.getLinkTable() != null && link.getLinkColumn() != null && isTrue(link.getLinkSave())
					&& isTrue(link.getIsList()) && link.getLinkTable().getColumnList() != null)) {
				continue;
			}
			String linkTableName = getLinkPropName(link);
			builder.append(",").append(linkTableName).append("List");
		}
		if (AutoUtil.hasColumn(columns, Constant.NEW_PARENT_ID)) {
			builder.append(",children");
		}
		String back = builder.toString();
		if (back.startsWith(",")) {
			back = back.substring(1);
			return "showFlags: '" + back + "', ";
		}
		return "";
	}

	private String removeLine(String fileTxt, int tab, String line) {
		int start = fileTxt.indexOf(tabStr(tab) + line);
		if (start > 0) {
			String temp = fileTxt.substring(start);
			temp = temp.substring(0, temp.indexOf("\n") + 1);
			return fileTxt.replace(temp, "");
		}
		return fileTxt;
	}

	private String removeLine(String fileTxt, String line) {
		return fileTxt.replace(line + "\n", "");
	}

	public String tabStr(int tab) {
		if (tab <= 0) {
			return "";
		}
		StringBuffer buffer = new StringBuffer();
		for (int i = 1; i <= tab; i++) {
			buffer.append("  ");
		}
		return buffer.toString();
	}

	private String getSpecialColumn(int tab) {
		String clnName; FormType formType;
		StrBuf crudSchemas = new StrBuf(true);
		for (Column cln : columns) {
			clnName = AutoUtil.firstLowerBean(cln.getName());
			formType = FormType.find(cln.getFormType());

			// 表格和输入都不显示不输出
			if (!isTrue(cln.getListShow())) {
				continue;
			}
			if (formType == FormType.IMAGE) {
				crudSchemas.append(tab, "<template v-if=\"column.key === '{1}'\">", clnName);
				crudSchemas.append(tab, "  <a-image :width=\"60\" :src=\"record.{1}\" />", clnName);
				crudSchemas.append(tab, "</template>");
			}

			if (cln.getName().endsWith("_ids")) {
				crudSchemas.append(tab, "<template v-if=\"column.key === '{1}'\">", clnName);
				crudSchemas.append(tab, "  <a-tag v-for=\"(item, index) in record.{1}\" :key=\"index\" color=\"green\">",
						AutoUtil.firstLowerBean(cln.getName().replace("_ids", "_list")));
				crudSchemas.append(tab, "    {{ item.name }}");
				crudSchemas.append(tab, "  </a-tag>");
				crudSchemas.append(tab, "</template>");
			}
		}
		return crudSchemas.moveLast().toString();
	}

	// 获取查询表单
	private String getFormItem(Column cln, String priKey, int tab, boolean isCascade) {
		StrBuf formShemas = new StrBuf(true);
		String clnCmmt = getCmmt(cln, "").replace("ID集合", "列表").replaceAll("\\[.*\\]", "");
		ClnType clnType = ClnType.find(cln.getType());
		String clnName = AutoUtil.firstLowerBean(cln.getName());
		FormType formType = FormType.find(cln.getFormType());

		// 父媒体框
		//render: ({ model, field }) => {
		//        return h(Tinymce, {
		//          value: model[field],
		//          onChange: (value: string) => {
		//            model[field] = value;
		//          },
		//        });
		//      },
		// formShemas.append(tab + 1, "{");
		if (isCascade) {
			formShemas.append(tab + 2, "title: '{1}',", clnCmmt);
			formShemas.append(tab + 2, "dataIndex: '{1}',", clnName);
		} else {
			formShemas.append(tab + 2, "label: '{1}',", clnCmmt);
			formShemas.append(tab + 2, "field: '{1}',", clnName);
		}
		if (isCascade && isTrue(cln.getFormShow())) {
			formShemas.append(tab + 2, "editRow: true,");
			// 编辑必填
			if (isTrue(cln.getFormShow()) && isTrue(cln.getRequired()) && !isCascade) {
				formShemas.append(tab + 2, "editRule: true,");
			}
		}
		// 自增ID长度80
		if (priKey.equals(cln.getName())) {
			formShemas.append(tab + 2, "width: 80,");
		}
		// 必填
		if (isTrue(cln.getFormShow()) && isTrue(cln.getRequired()) && !isCascade) {
			formShemas.append(tab + 2, "required: true,");
		}
		// 表单不显示显示
		if (!isTrue(cln.getFormShow()) && !isCascade) {
			formShemas.append(tab + 2, "form: { show: false },");
		}
		// 表单属性
		else if (isTrue(cln.getFormShow())) {
			// 默认值
			String defaultValue = "";
			if (clnType == ClnType.BOOLEAN) {
				formShemas.append(tab + 2, "width: {1},", isCascade ? 110 : 80);
				formShemas.append(tab + 2, "component: 'RadioButtonGroup',");
				formShemas.append(tab + 2, "componentProps: {");
				formShemas.append(tab + 3, "options: [");
				formShemas.append(tab + 4, "{ label: '否', value: false },");
				formShemas.append(tab + 4, "{ label: '是', value: true }");
				formShemas.append(tab + 3, "]");
				formShemas.append(tab + 2, "},");
				formShemas.append(tab + 2, "customRender: ({ record }) => {");
				formShemas.append(tab + 3, "return h(Tag, { color: record?.{1} ? 'green' : 'red' }, () => record?.{1} ? '是' : '否');", clnName);
				formShemas.append(tab + 2, "},");
			}
			else if (!StrUtil.isEmpty(cln.getLimits())) { // && !cln.getName().equals(Constant.CLN_STATUS)
				formShemas.append(tab + 2, "width: {1},", isCascade ? 120 : 100);
				formShemas.append(tab + 2, "component: 'Select',");
				formShemas.append(tab + 2, "componentProps: {");
				formShemas.append(tab + 3, "options: [");
				formShemas.append(getLimitsHtml(clnName, cln.getLimits(), tab + 4));
				formShemas.append(tab + 3, "],");
				if (!(cln.getDefVal() != null && !cln.getDefVal().equals("NULL") && !isTrue(cln.getRequired()))
					&& (cln.getLimits().contains(":") || cln.getLimits().contains(","))) {
					Limits limits = new Limits(cln.getLimits());
					Limits.Dict dict = limits.getDictList().get(0);
					if (dict.getIntKey() == null) {
						defaultValue = StrBuf.format("defaultValue: '{1}',", dict.getStrKey());
					} else {
						defaultValue = StrBuf.format("defaultValue: {1},", dict.getIntKey());
					}
				}
				if (isCascade) {
					formShemas.append(tab + 3, defaultValue);
					defaultValue = null;
				}
				formShemas.append(tab + 2, "},");
			}
			else if (formType == FormType.IMAGE || formType == FormType.FILE) {
				formShemas.append(tab + 2, "width: {1},", isCascade ? 110 : 80);
				formShemas.append(tab + 2, "component: 'Upload',");
				formShemas.append(tab + 2, "componentProps: {");
				formShemas.append(tab + 3, "api: uploadApi,");
				formShemas.append(tab + 3, "maxNumber: 1,");
				formShemas.append(tab + 2, "},");
			}
			else if (clnType == ClnType.DATE) {
				formShemas.append(tab + 2, "width: 120,");
				formShemas.append(tab + 2, "component: 'DatePicker',");
				if (formType == FormType.DATE) {
					formShemas.append(tab + 2, "componentProps: {");
					formShemas.append(tab + 3, "format: 'YYYY-MM-DD',");
					formShemas.append(tab + 3, "valueFormat: 'YYYY-MM-DD',");
					formShemas.append(tab + 2, "},");
				} else if (formType == FormType.TIME) {
					formShemas.append(tab + 2, "componentProps: {");
					formShemas.append(tab + 3, "format: 'YYYY-MM-DD HH:mm:ss',");
					formShemas.append(tab + 3, "valueFormat: 'YYYY-MM-DD HH:mm:ss',");
					formShemas.append(tab + 3, "showTime: { format: 'HH:mm:ss' },");
					formShemas.append(tab + 2, "},");
				}
			}
			// 级联查找 [getData.params]
			else if (formType == FormType.SELECT) { // || formType == FormType.MY_CONF
				formShemas.append(tab + 2, "component: 'ApiSelect',");
				formShemas.append(tab + 2, "componentProps: {");
				formShemas.append(tab + 3, "api: async (params) => await offsetBy('{1}.{2}', { pageSize: 10, showFields: 'id,name', params }),"
						, cln.getLinkTable().getModule().getName(), AutoUtil.firstLowerBean(cln.getLinkTable().getName()));
				formShemas.append(tab + 3, "labelField: 'name',");
				formShemas.append(tab + 3, "valueField: 'id',");
				if (cln.getName().endsWith("_ids")) {
					formShemas.append(tab + 3, "mode: 'multiple',");
				}
				formShemas.append(tab + 2, "},");
				//TODO editRender: ({ record }) => record?.implPhase?.name ?? record?.implPhaseId,
			} else if (cln.isNumber() || cln.isDecimal()) {
				formShemas.append(tab + 2, "width: 110,");
				formShemas.append(tab + 2, "component: 'InputNumber',");
			}
			else if (isShowInDetail(cln)) {
				if (isCascade) {
					formShemas.append(tab + 2, "component: 'Input',");
				} else {
					formShemas.append(tab + 2, "component: 'InputTextArea',");
				}
			}
			else {
				formShemas.append(tab + 2, "component: 'Input',");
			}
			if (!isTrue(cln.getNotNull()) && cln.getDefVal() != null && StrUtil.isNotEmpty(cln.getDefVal())
					&& !cln.getDefVal().equals("NULL") && !isTrue(cln.getRequired())) {
				if (clnType == ClnType.BOOLEAN) {
					defaultValue = StrBuf.format("defaultValue: {1},", cln.getDefVal().equals("1"));
				} else if (cln.isNumber() || cln.isDecimal()) {
					defaultValue = StrBuf.format("defaultValue: {1},", cln.getDefVal());
				} else {
					defaultValue = StrBuf.format("defaultValue: '{1}',", cln.getDefVal().equals("空") ? "" : cln.getDefVal());
				}
			}
			if (table.getRowCol() != null && table.getRowCol() == 2 && !isShowInDetail(cln) && !isCascade) {
				formShemas.append(tab + 2, "colProps: { span: 12 },");
				if (StrUtil.isNotEmpty(defaultValue)) {
					formShemas.append(tab + 2, defaultValue);
				}
			} else if (StrUtil.isNotEmpty(defaultValue)) {
				formShemas.append(tab + 2, defaultValue);
			}
		}
		return formShemas.moveLast().toString();
	}

	// 获取查询表单
	private String getCrudSchemas(String priKey, int tab) {
		String clnName, clnCmmt; ClnType clnType; FormType formType; QueryType qryType;
		StrBuf crudSchemas = new StrBuf(true);
		for (Column cln : columns) {
			clnCmmt = getCmmt(cln, "");
			clnType = ClnType.find(cln.getType());
			clnName = AutoUtil.firstLowerBean(cln.getName());
			formType = FormType.find(cln.getFormType());
			qryType = QueryType.find(cln.getQueryType());

			// 表格和输入都不显示不输出
			if (!isTrue(cln.getFormShow()) && !isTrue(cln.getListShow())) {
				continue;
			}

			crudSchemas.append(tab + 1, "{");
			crudSchemas.append(getFormItem(cln, priKey, tab, false) + "\n");
			// 表格不显示
			if (!isTrue(cln.getListShow()) || ColumnTypeEnum.find(cln.getType()) == ColumnTypeEnum.Text
					|| ColumnTypeEnum.find(cln.getType()) == ColumnTypeEnum.Blob) {
				crudSchemas.append(tab + 2, "table: { show: false },");
			} else {
				if (StrUtil.isNotEmpty(cln.getLimits())) {
					crudSchemas.append(tab + 2, "table: {");
					crudSchemas.append(tab + 3, "dataIndex: '{1}Text',", clnName);
					crudSchemas.append(tab + 2, "},");
				} else if (cln.getLinkTable() != null && !Constant.NEW_TENANT_ID.equals(clnName)) {
					String name = cln.getName().replace("_ids", "_list").replace("_id", "");
					crudSchemas.append(tab + 2, "table: {");
					crudSchemas.append(tab + 3, "dataIndex: ['{1}', 'name'],", AutoUtil.firstLowerBean(name));
					crudSchemas.append(tab + 2, "},");
				}
				// 搜索 必须在表格显示
				if (!(cln.getQueryType() == null || cln.getQueryType() < 1) || priKey.equals(cln.getName())) {
					crudSchemas.append(tab + 2, "search: {");
					if (qryType == QueryType.LIKE || qryType == QueryType.RIGHT_LIKE) {
						String newName = qryType == QueryType.LIKE ? clnName + "$Lk" : clnName + "$LkRt";
						crudSchemas.append(tab + 3, "field: '{1}',", newName);
					} else if (qryType == QueryType.RANGE_EQUALS || qryType == QueryType.RANGE_NOT_EQUALS) {
						crudSchemas.append(tab + 3, "field: '{1}',",
								qryType == QueryType.RANGE_EQUALS ? clnName + "$Range" : clnName + "$RangeNe");
					}
					// search的component
					if (qryType == QueryType.RANGE_EQUALS || qryType == QueryType.RANGE_NOT_EQUALS) {
						if (formType == FormType.TIME || formType == FormType.DATE) {
							crudSchemas.append(tab + 3, "component: 'RangePicker',");
							crudSchemas.append(tab + 3, "componentProps: {");
							crudSchemas.append(tab + 4, "format: 'YYYY-MM-DD HH:mm:ss',");
							if (formType == FormType.TIME) {
								crudSchemas.append(tab + 4, "showTime: { format: 'HH:mm:ss' },");
							}
							crudSchemas.append(tab + 4, "placeholder: ['开始时间', '结束时间'],");
							crudSchemas.append(tab + 3, "},");
						} else if (cln.isNumber() || cln.isDecimal()) {
							// crudSchemas.append(tab + 3, "field: 'errNum$Range',");
						 	crudSchemas.append(tab + 3, "component: 'RangeNumber',");
						}
					} else if (isShowInDetail(cln)) {
						crudSchemas.append(tab + 3, "component: 'Input',");
					}
					crudSchemas.append(tab + 3, "colProps: { span: 6 },");
					crudSchemas.append(tab + 2, "},");
				}
			}

			crudSchemas.append(tab + 1, "},");
		}
		return crudSchemas.moveLast().toString();
	}

	private boolean isShowInDetail(Column cln) {
		ClnType clnType = ClnType.find(cln.getType());
		return clnType == ClnType.TEXT || clnType == ClnType.BLOB || cln.getLength() > 255; //cln.getLength() >= 1024
	}

	// 限制html
	private String getLimitsHtml(String clnName, String limitStr, int tab) {
		Limits limits = new Limits(limitStr);
		StrBuf result = new StrBuf(true);
		for (Limits.Dict dict : limits.getDictList()) {
			String prev = dict.getIntKey() == null ? dict.getStrKey() : dict.getIntKey() + "";
			String next = dict.getValue();
			if (clnName.equals("status") && (prev.equals("DELETE") || prev.equals("0"))) {
				continue;
			}
			if (dict.getIntKey() == null) {
				result.append(tab,"{ label: '{1}', value: '{2}' },", next, prev);
			} else {
				result.append(tab,"{ label: '{1}', value: {2} },", next, prev);
			}
		}
		return result.toString();
	}

	public String fillByKey(String fileTxt, String start, String end, String value) {
		int startIndex = fileTxt.indexOf(start);
		if (startIndex > -1) {
			String temp = fileTxt.substring(startIndex);
			int endIndex = temp.indexOf(end);
			if (endIndex > -1) {
				temp = temp.substring(0, endIndex);
				fileTxt = fileTxt.replace(temp, start + "\n" + value + "\n");
			}
		}
		return fileTxt;
	}

	public String getCmmt(Column cln, String last) {
		String comment = cln.getCmmt().replaceAll("\\[.*\\]", "");
		if (last != null && cln.getLinkTable() != null && comment.endsWith("ID")) {
			return comment.substring(0, comment.length() - 2) + last;
		}
		return comment;
	}
}
